/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Panel;
import com.jb.web.bean.Label;
import com.jb.web.bean.TabbedPanel;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;

public class AccordionRenderer extends RegionRenderer {
    public StringBuffer renderChildren(WebContext ctx, 
                                    UIComponent itemToRender) {
        TabbedPanel sect = (TabbedPanel)itemToRender;
        StringBuffer html = new StringBuffer();
        Iterator iter = sect.getChildPanels().iterator();
        StringBuffer options = new StringBuffer("[");
        while (iter.hasNext()) {
            Panel aSect = (Panel)iter.next();
            options.append("'").append(aSect.getClientId()).append("'");
            if(iter.hasNext())
                options.append(",");
        }
        options.append("]");
        //Accordion.touch ('mail',['mail','notes','contacts']);
        iter = sect.getChildPanels().iterator();
        while (iter.hasNext()) {
            Panel aSect = (Panel)iter.next();
            if (aSect != null) {
                Label aTitle = aSect.getHeader();
                String label = evaluate(ctx, aTitle, aTitle.getValue());
                if (isEmptyString(label)) {
                    label = aSect.getId();
                }
                String headStyle = aTitle.getStyle(ctx);
                html.append("<div ");
                html.append(getEditScript(ctx, aTitle))
                    .append(addTagProperty("class", aTitle.getStyleClass(), null))
                    .append(addTagProperty("style", headStyle, null))
                
                .append(" onclick=").append("\"Accordion.touch('").append(aSect.getClientId()).append("',")
                .append(options).append(");\"")
                    
                .append(">");

                if (!isEmptyString(aTitle.getIconImage())) {
                    html.append("<img  class=\"jbLabelImg\" ")
                        .append(addTagProperty("src", aTitle.getIconImage(), null)).append(" />");
                }
                html.append(label);
                html.append("</div>");
                if(!sect.isSelected(aSect))
                    aSect.setDisplayStyle("none");
                html.append(aSect.render(ctx));
                if(!sect.isSelected(aSect))
                    aSect.setDisplayStyle("");
            }
        }
        return html;
    }
}
